package co.com.umobile.gui;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import co.com.umobile.R;
import co.com.umobile.application.GlobalState;

public class MenuActivity extends Activity {

	/** the grid views. */
	private View[] listView = new View[4];

	/** the home container. */
	private GridView gridViewHome;

	/** the imageWidth. */
	private int imageWidth;

	/** globalState is used to transfer data to other activity. */
	private GlobalState globalState;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

		globalState = (GlobalState) getApplication();

		ActionBar actionBar = getActionBar();
		actionBar.setTitle(R.string.home_title);

		setContentView(R.layout.menu_layout);

		gridViewHome = (GridView) findViewById(R.id.gridViewHome);

		int orientation = getResources().getConfiguration().orientation;

		if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
			int parentWidth = (getWindowManager().getDefaultDisplay().getWidth() - (3 * (int) getResources()
					.getDimension(R.dimen.padding_menu))) / 3;
			imageWidth = (parentWidth / 3);
			gridViewHome.setNumColumns(2);
		} else {
			int parentWidth = (getWindowManager().getDefaultDisplay().getWidth() - (3 * (int) getResources()
					.getDimension(R.dimen.padding_menu))) / 3;
			imageWidth = (parentWidth / 3);
			gridViewHome.setNumColumns(2);
		}

		fillViews();
		gridViewHome.setAdapter(new MenuAdapter());

		gridViewHome.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				 showOption(position);
			}
		});

	}

	/**
	 * fill the views.
	 */
	private void fillViews() {

		listView[0] = getMenuItemView(R.drawable.consult_order_menu, R.string.home_option0, R.string.home_desc0, 0);
		listView[1] = getMenuItemView(R.drawable.map_menu, R.string.home_option1, R.string.home_desc1, 1);
		listView[2] = getMenuItemView(R.drawable.sync_menu_icon, R.string.home_option2, R.string.home_desc2, 2);
		listView[3] = getMenuItemView(R.drawable.configuration_menu, R.string.home_option3, R.string.home_desc3, 3);

	}

	/**
	 * shows an option.
	 * 
	 * @param position
	 *            position to set.
	 */
	private void showOption(int position) {

		switch (position) {
		case 0:
			
			Intent subjectIntent = new Intent(MenuActivity.this, SubjectListActivity.class);
			startActivity(subjectIntent);

			break;
		case 1:
			Intent placeIntent = new Intent(MenuActivity.this, PlaceListActivity.class);
			startActivity(placeIntent);
			break;

		case 2:
			Intent notificationIntent = new Intent(MenuActivity.this, NotificationListActivity.class);
			startActivity(notificationIntent);
			break;

		case 3:
			//Sync
	        Intent syncIntent = new Intent(MenuActivity.this, UpdatesActivity.class);
	        startActivity(syncIntent);
			break;

		default:
			break;
		}

	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		 switch (item.getItemId()) {
	      case android.R.id.home:
	        finish();
	        break;
	      default:
	        return super.onOptionsItemSelected(item);
	    }

	    return true;
	}

	/**
	 * Gets the menu item view.
	 * 
	 * @param imageId
	 *            Image id.
	 * @param titleId
	 *            title id.
	 * @param descriptionId
	 *            Description i.
	 * @param position
	 *            Position.
	 * @return {@link View}
	 */
	private View getMenuItemView(int imageId, int titleId, int descriptionId, final int position) {

		View view = getLayoutInflater().inflate(R.layout.menu_item_layout, null);

		ImageView imageView = (ImageView) view.findViewById(R.id.imageViewMenu);

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(imageWidth, imageWidth);
		imageView.setLayoutParams(layoutParams);
		imageView.setImageResource(imageId);

		((TextView) view.findViewById(R.id.textViewMenuTitle)).setText(titleId);
		((TextView) view.findViewById(R.id.textViewMenuDescription)).setText(descriptionId);

		view.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				 showOption(position);
			}
		});

		return view;

	}

	private class MenuAdapter extends BaseAdapter {

		public int getCount() {
			return listView.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			return listView[position];
		}

	}

	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
		window.addFlags(WindowManager.LayoutParams.FLAG_DITHER);
	}

}
